package com.example.annolog.interceptor;

import com.example.annolog.anno.Log;
import com.example.annolog.utils.ReflectUtils;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.lang.invoke.MethodHandle;

@Component
public class LogInterceptorInvok implements MethodInterceptor {
    @Nullable
    @Override
    public Object invoke(@Nonnull MethodInvocation invocation) throws Throwable {
        Log annotation = ReflectUtils.getAnnotationFromInvocation(invocation.getMethod(), Log.class);
        if (annotation == null) {
            return invocation.proceed();
        }
        // 为什么调用http://127.0.0.1:8089/jpademo/perform时有两次输出呢？
        // 因为在Audience里面用的是@Around，会拦截到两次
        System.out.println("method " + invocation.getMethod() + " is called on " + invocation.getThis() + " with args" +
                " " + invocation.getArguments());
        Object proceed = invocation.proceed();
        System.out.println("method " + invocation.getMethod() + " returns " + proceed);
        return proceed;


    }
}
